{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# opencv\n",
    "\n",
    "**以 RTP-TCP 方式读取视频流**\n",
    "\n",
    "pip instapp opencv-python\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "\n",
    "cap = cv2.VideoCapture('rtsp://admin:admin777@10.86.77.12:554/h264/ch1/sub/av_stream')\n",
    "\n",
    "while (1):\n",
    "    ret, img = cap.read()\n",
    "    if not ret:\n",
    "        print(\"Can't receive frame (stream end?). Exiting ...\")\n",
    "        break\n",
    "    cv2.imshow(\"Image\", img)\n",
    "    if cv2.waitKey(1) & 0xFF == ord('q'):\n",
    "        break\n",
    "cap.release()  # 释放摄像头\n",
    "cv2.destroyAllWindows()  # 释放窗口资源"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# pyav\n",
    "\n",
    "**以 RTP-UDP 方式读取视频流**\n",
    "\n",
    "文档：<http://docs.mikeboers.com/pyav/develop/>\n",
    "\n",
    "```sh\n",
    "conda install av -c conda-forge\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Users\\13438\\Desktop\n"
     ]
    }
   ],
   "source": [
    "cd C:\\Users\\13438\\Desktop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "deprecated pixel format used, make sure you did set range correctly\n",
      " (repeated 10 more times)\n",
      "deprecated pixel format used, make sure you did set range correctly\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "container: <av.InputContainer 'C:\\\\Users\\\\13438\\\\Desktop\\\\rtp_h264.h264'>\n",
      "container.streams: <av.container.streams.StreamContainer object at 0x00000295DAD5CD68>\n",
      "container.format: <av.ContainerFormat 'h264'>\n"
     ]
    }
   ],
   "source": [
    "# 读文件\n",
    "\n",
    "import av\n",
    "\n",
    "inputFileName = r\"C:\\Users\\13438\\Desktop\\rtp_h264.h264\"\n",
    "container = av.open(inputFileName)\n",
    "\n",
    "print(\"container:\", container)\n",
    "print(\"container.streams:\", container.streams)\n",
    "print(\"container.format:\", container.format)\n",
    "\n",
    "for frame in container.decode(video=0):\n",
    "    # 将每一帧保存为图片\n",
    "    frame.to_image().save('frame-%04d.jpg' % frame.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "deprecated pixel format used, make sure you did set range correctly\n",
      " (repeated 15 more times)\n",
      "'circular_buffer_size' option was set but it is not supported on this build (pthread support is required)\n",
      "'circular_buffer_size' option was set but it is not supported on this build (pthread support is required)\n",
      "D:\\SoftInstall\\Anacoda3\\lib\\site-packages\\ipykernel_launcher.py:13: AttributeRenamedWarning: VideoFrame.to_nd_array is deprecated; please use VideoFrame.to_ndarray.\n",
      "  del sys.path[0]\n",
      "deprecated pixel format used, make sure you did set range correctly\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "format:Input #0, rtsp, from '':\n",
      "  Metadata:\n",
      "    title           : Media Presentation\n",
      "  Duration: N/A, start: 0.240000, bitrate: N/A\n",
      "    Stream #0:0: Video: h264 (Main), yuvj420p(pc, bt709, progressive), 704x576, 25 fps, 25 tbr, 90k tbn, 50 tbc\n",
      "\n",
      "frame:0\n",
      "frame:1\n",
      "frame:2\n",
      "frame:3\n",
      "frame:4\n",
      "frame:5\n",
      "frame:6\n",
      "frame:7\n",
      "frame:8\n",
      "frame:9\n",
      "frame:10\n",
      "frame:11\n",
      "frame:12\n",
      "frame:13\n",
      "frame:14\n",
      "frame:15\n",
      "frame:16\n",
      "frame:17\n",
      "frame:18\n",
      "frame:19\n",
      "frame:20\n",
      "frame:21\n",
      "frame:22\n",
      "frame:23\n",
      "frame:24\n",
      "frame:25\n",
      "frame:26\n",
      "frame:27\n",
      "frame:28\n",
      "frame:29\n",
      "frame:30\n",
      "frame:31\n",
      "frame:32\n",
      "frame:33\n",
      "frame:34\n",
      "frame:35\n",
      "frame:36\n",
      "frame:37\n",
      "frame:38\n",
      "frame:39\n",
      "frame:40\n",
      "frame:41\n",
      "frame:42\n",
      "frame:43\n",
      "frame:44\n",
      "frame:45\n",
      "frame:46\n",
      "frame:47\n",
      "frame:48\n",
      "frame:49\n",
      "frame:50\n",
      "frame:51\n",
      "frame:52\n",
      "frame:53\n",
      "frame:54\n",
      "frame:55\n",
      "frame:56\n",
      "frame:57\n",
      "frame:58\n",
      "frame:59\n",
      "frame:60\n",
      "frame:61\n",
      "frame:62\n",
      "frame:63\n",
      "frame:64\n",
      "frame:65\n",
      "frame:66\n",
      "frame:67\n",
      "frame:68\n",
      "frame:69\n"
     ]
    }
   ],
   "source": [
    "# 读取网络地址\n",
    "import av\n",
    "import cv2\n",
    "\n",
    "\n",
    "video = av.open('rtsp://admin:tsit2019@192.168.201.14:554/h264/ch1/sub/av_stream', 'r')\n",
    "print(\"format:\" + video.dumps_format())\n",
    "\n",
    "try:\n",
    "    for frame in video.decode():\n",
    "        print(\"frame:{}\".format(frame.index))\n",
    "\n",
    "        img = frame.to_nd_array(format='bgr24')\n",
    "        cv2.imshow(\"Test\", img)\n",
    "#         cv2.imwrite(\"Test{}.jpg\".format(frame.index), img)\n",
    "\n",
    "        if cv2.waitKey(1) & 0xFF == ord('q'):\n",
    "            break\n",
    "except Exception as e:\n",
    "    print('fate erro:{}'.format(e))\n",
    "finally:\n",
    "    cv2.destroyAllWindows()\n",
    "    video.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# imageio\n",
    "\n",
    "# python-vlc"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
